﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.Controls;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Base.Menu;
using MicropleSoft.WmsApp.Base.Window;
using MicropleSoft.WmsApp.Library.Security;
using MicropleSoft.WmsApp.Library.ServiceFacade;
using MicropleSoft.WmsApp.SecurityAuthentication;

namespace MicropleSoft.WmsApp
{
    public partial class FormMain : DevExpress.XtraEditors.XtraForm, IFormMain
    {
        #region 私有控件变量
        private XBarSubItem _xBarSubItemFile;
        private XBarSubItem _xBarSubItemEdit;
        private XBarSubItem _xBarSubItemView;
        private XBarSubItem _xBarSubItemBusiness;
        private XBarSubItem _xBarSubItemTool;
        private XBarSubItem _xBarSubItemService;
        private XBarSubItem _xBarSubItemHelp;
        #endregion

        #region 私有变量
        private readonly IAuthentication _authentication = new Authentication();
        #endregion

        public FormMain()
        {
            InitializeComponent();
            RegisterTreeListNodeRelateForm();
            CreateXBarSubItems();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Login();
            MicropleSoft.Controls. Common.FormMainSingleInstance = this;

            var assembly = Assembly.LoadFile(Application.StartupPath + "\\MicropleSoft.WmsApp.PurchaseOrder.dll");
            if (assembly != null)
            {
                var form = (BaseForm)assembly.CreateInstance("MicropleSoft.WmsApp.PurchaseOrder.FormPurchaseOrder");
                form.HeadText = "采购订单";
                this.AddOwnedForm(form);
                form.MdiParent = this;
                form.Show();
                form.Activate();
            }

            //var assembly = Assembly.LoadFile(Application.StartupPath + "\\MicropleSoft.WmsApp.BaseDataManage.dll");
            //if (assembly != null)
            //{
            //    var form = (BaseForm)assembly.CreateInstance("MicropleSoft.WmsApp.BaseDataManage.FormBaseDataMain");
            //    form.HeadText = "静态数据维护";
            //    this.AddOwnedForm(form);
            //    form.MdiParent = this;
            //    form.Show();
            //    form.Activate();
            //}
        }

        private void RegisterTreeListNodeRelateForm()
        {
            this.xTreeListMain.DoubleClick += (o, e) =>
                                                  {
                                                      if (this.xTreeListMain.FocusedNode != null
                                                          && !this.xTreeListMain.FocusedNode.HasChildren
                                                          && this.xTreeListMain.FocusedNode.Tag != null
                                                          && this.xTreeListMain.FocusedNode.Tag is MenuItemModel)
                                                      {
                                                          var model =
                                                              this.xTreeListMain.FocusedNode.Tag as MenuItemModel;
                                                          string[] url = model.Url.Split(',');
                                                          if (url.Length < 2)
                                                          {
                                                              MicropleSoft.Controls.DialogBox.CustomMessageBox.Show("URL错误：" + model.Url,
                                                                                                                        CustomMessageBox.MsgBoxButtons.Ok,
                                                                                                                    CustomMessageBox.MsgBoxIcons.Error);
                                                              return;
                                                          }
                                                          string file = url[1];
                                                          string type = url[0];
                                                          file = Application.StartupPath + "\\" + file + ".dll";
                                                          if (!File.Exists(file))
                                                          {
                                                              MicropleSoft.Controls.DialogBox.CustomMessageBox.Show("文件不存在！",
                                                                                                                    CustomMessageBox.MsgBoxButtons.Ok,
                                                                                                                CustomMessageBox.MsgBoxIcons.Error);
                                                              return;
                                                          }
                                                          var assembly = Assembly.LoadFile(file);
                                                          if (assembly != null)
                                                          {
                                                              var form = (BaseForm)assembly.CreateInstance(type);
                                                              form.HeadText = model.Caption;
                                                              this.AddOwnedForm(form);
                                                              form.MdiParent = this;
                                                              form.Show();
                                                              form.Activate();
                                                          }
                                                      }
                                                  };
        }

        #region CreateXBarSubItem
        private void CreateXBarSubItems()
        {
            _xBarSubItemFile = new XBarSubItem();
            _xBarSubItemEdit = new XBarSubItem();
            _xBarSubItemView = new XBarSubItem();
            _xBarSubItemBusiness = new XBarSubItem();
            _xBarSubItemTool = new XBarSubItem();
            _xBarSubItemService = new XBarSubItem();
            _xBarSubItemHelp = new XBarSubItem();

            this.barManagerMain.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
            this._xBarSubItemFile,
            this._xBarSubItemEdit,
            this._xBarSubItemView,
            this._xBarSubItemBusiness,
            this._xBarSubItemTool,
            this._xBarSubItemService,
            this._xBarSubItemHelp});

            this.barMainMenu.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemFile),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemEdit),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemView),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemBusiness),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemTool),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemService),
            new DevExpress.XtraBars.LinkPersistInfo(this._xBarSubItemHelp)});

            // 
            // xBarSubItemFile
            // 
            this._xBarSubItemFile.Caption = "系统(&F)";
            this._xBarSubItemFile.Id = 0;
            this._xBarSubItemFile.Name = "xBarSubItemFile";
            // 
            // xBarSubItemEdit
            // 
            this._xBarSubItemEdit.Caption = "编辑(&E)";
            this._xBarSubItemEdit.Id = 1;
            this._xBarSubItemEdit.Name = "xBarSubItemEdit";
            // 
            // xBarSubItemView
            // 
            this._xBarSubItemView.Caption = "查看(&V)";
            this._xBarSubItemView.Id = 2;
            this._xBarSubItemView.Name = "xBarSubItemView";
            // 
            // xBarSubItemBusiness
            // 
            this._xBarSubItemBusiness.Caption = "业务(&O)";
            this._xBarSubItemBusiness.Id = 3;
            this._xBarSubItemBusiness.Name = "xBarSubItemBusiness";
            // 
            // xBarSubItemTool
            // 
            this._xBarSubItemTool.Caption = "工具(&T)";
            this._xBarSubItemTool.Id = 4;
            this._xBarSubItemTool.Name = "xBarSubItemTool";
            // 
            // xBarSubItemService
            // 
            this._xBarSubItemService.Caption = "服务(&S)";
            this._xBarSubItemService.Id = 5;
            this._xBarSubItemService.Name = "xBarSubItemService";
            // 
            // xBarSubItemHelp
            // 
            this._xBarSubItemHelp.Caption = "帮助(&H)";
            this._xBarSubItemHelp.Id = 6;
            this._xBarSubItemHelp.Name = "barSubItemHelp";
        }
        #endregion

        private void RefreshData()
        {
            try
            {
                TreeListBinder.BindingTreeListMenu(this.xTreeListMain);
            }
            catch (Exception exception)
            {
                CustomMessageBox.Show(exception.Message,
                                      CustomMessageBox.MsgBoxButtons.Ok, CustomMessageBox.MsgBoxIcons.Error);
            }
         
        }

        private void Login()
        {
            bool result = _authentication.Login(this);
            if (!result)
            {
                Application.Exit();
            }
            else
            {
                RefreshData();
            }
        }
    }
}